/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package web.models;

import entities.Category;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.model.ListDataModel;
import org.primefaces.model.SelectableDataModel;

/**
 *
 * @author Jesus
 */
@ManagedBean
public class CategoryModel extends ListDataModel<Category> implements SelectableDataModel<Category> {

    public CategoryModel() {
    }

    public CategoryModel(List<Category> categories) {
        super(categories);
    }

    @Override
    public Object getRowKey(Category category) {
        return category.getId();
    }

    @Override
    public Category getRowData(String idCategory) {
        System.out.println("getRowData:CAT --> " + idCategory);
        if (idCategory != null) {
            Short id = Short.valueOf(idCategory);
            for (Category cat : (List<Category>) getWrappedData()) {
                if (cat.getId().equals(id)) {
                    return cat;
                }
            }
        }
        return null;
    }

    public boolean add(Category category) {
        return ((List<Category>) getWrappedData()).add(category);
    }
}
